package com.games.portal.mng;

import java.io.File;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

import com.games.portal.rest.model.Game;
import com.games.portal.rest.model.Games;

public class XmlParserManager {

	public static Games unmarshallGamesXml()  {
		JAXBContext jaxbContext;
		Unmarshaller unmarshaller = null;
		Games gameList = new Games();
		gameList.setGames(new ArrayList<Game>());
		File file = new File("../webapps/GamesWebService/resources/game-list.xml");
		try {
			jaxbContext = JAXBContext.newInstance(Games.class);
			unmarshaller = jaxbContext.createUnmarshaller();
			unmarshaller.setEventHandler(
					new ValidationEventHandler() {
						public boolean handleEvent(ValidationEvent event ) {
							System.err.println("Error in unmarshalling xml: "+ event.getMessage() + " " + event.getLinkedException());
							return false;
						}
					});
			gameList = (Games) unmarshaller.unmarshal(file);
		} catch (JAXBException e) {
			System.err.println("Error in unmarshalling xml: "+e.getMessage());		
		}
		return gameList;
	}

}
